<?php

class MemberplansController extends AppController {

    var $name = 'Memberplans';
    var $components = array('Auth', 'Session', 'RequestHandler');
    var $helper = array('Js', 'Ajax', 'Javascript');
    var $uses = array('Memberplan', 'Memberplantext');
    var $paginate = array(
        'limit' => '10',
        'order' => array('order' => 'desc'),
    );

    function beforeFilter() {
        parent::beforeFilter();
        //$this->Auth->allow();
        $this->Auth->loginRedirect = array('controller' => 'Dashboard', 'action' => 'index');
    }

    function admin_index() {
        $data = $this->Paginate('Memberplan');
        $this->set('fields', $data);

        $text = $this->Memberplantext->find('all');
        $this->set('text', $text);
    }

    function admin_add() {
        error_reporting(0);
        if (!empty($this->data['period'])) {
            $count = 0;
            foreach ($this->data['period'] as $data) {
                $points.=$data['name'] . ',';
                $price.=$this->data['price'][$count]['name'] . ',';
                $count++;
            }
            $this->data['Memberplan']['price'] = $price;
            $this->data['Memberplan']['period'] = $points;
//            $this->data['Memberplan']['option1'] = $this->data['Memberplan']['option1Array'];
//            $this->data['Memberplan']['option2'] = $this->data['Memberplan']['option2Array'];
//            $this->data['Memberplan']['option3'] = $this->data['Memberplan']['option3Array'];
            if ($this->Memberplan->save($this->data)) {
                $this->Session->setFlash("Member Plan Added.");
                $this->redirect(array('controller' => 'memberplans', 'action' => 'index'));
            }
        }
    }

    function admin_edit($id=null) {
        error_reporting(0);

        if (!empty($this->data['period'])) {
            $count = 0;
            foreach ($this->data['period'] as $data) {
                $points.=$data['name'] . ',';
                $price.=$this->data['price'][$count]['name'].',';
                $count++;
           
             }
            $this->data['Memberplan']['price'] = $price;
            $this->data['Memberplan']['period'] = $points;
            $this->data['Memberplan']['id']=$id;
            if ($this->Memberplan->save($this->data)) {
                $this->Session->setFlash("Member Plan Edited.");
                $this->redirect(array('controller' => 'memberplans', 'action' => 'index'));
            }
        }
        $this->data = $this->Memberplan->read(null, $id);
        //debug($this->data);
    }

    function admin_update_text() {
        $this->autoRender = false;
        if (!empty($this->data)) {
            $this->Memberplantext->id = $this->data['Memberplan']['id'];
            if ($this->Memberplantext->save()) {
                $this->Session->setFlash("Plan Text saved successfully.");
                $this->redirect(array('controller' => 'Memberplans', 'action' => 'index'));
            }
        }
    }

    function admin_sorted() {
        Configure::write('debug', '0');
        $this->layout = '';
        if ($this->RequestHandler->isAjax()) {
            $this->autoRender = false;
            $array = array();

            $array = $_POST['recordsArray'];
            if ($_POST['update'] == "update") {
                $count = 1;
                foreach ($array as $idval) {

                    $query = "UPDATE `memberplans` SET `order`= '$count' WHERE `id`= '$idval'";
                    mysql_query($query) or die(mysql_error() . 'Error, insert query failed');
                    $count++;
                }
                echo 'All saved! refresh the page to see the changes';
            }
        }
    }

    function admin_delete($id=null) {
        if ($this->Memberplan->delete($id)) {
            $this->Session->setFlash("Memberplan deleted.");
            $this->redirect(array('controller' => 'memberplans', 'action' => 'index'));
        }
    }

}

?>
